// 路由鉴权
import router from "@/router";
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {useAdminStore} from "@/stores/admin.ts";
import {useMemberStore} from "@/stores/member.ts";

NProgress.configure({showSpinner: false})

// 全局前置守卫
router.beforeEach((to, from) => {
    document.title = '健身中心-' + to.meta.title
    NProgress.start()

    const adminStore = useAdminStore()
    const memberStore = useMemberStore()

    const adminToken = adminStore.adminInfo.token
    const memberToken = memberStore.memberInfo.token

    if (to.meta.admin) {
        if (adminToken) {
            if (to.path == '/adminLogin') {
                return '/admin'
            }
            return true
        } else {
            // 避免从 /adminLogin 重定向到 /adminLogin
            if (to.path !== '/adminLogin') {
                return '/adminLogin';
            }
            return true;
        }
    } else {
        if (memberToken) {
            if (to.path == '/memberLogin') {
                return 'member1'
            }
            return true
        } else {
            // 避免从 /memberLogin 重定向到 /memberLogin
            if (to.path !== '/memberLogin' && to.path !== '/register') {
                return '/memberLogin';
            }
            return true;
        }
    }
})

// 全局后置守卫
router.afterEach((to, from) => {
    NProgress.done()
})